You can always find the latest version on my web page: http://www.infoworkshop.com/~jonpugh/
Free for noncommercial use.
Contact me for a simple cheap license if you wish to include any or all of these as part of a software solution that you sell.
This AppleScript scripting addition provides AppleScript and all scriptable and Apple Event aware applications with added functionality. To examine the AppleScript syntax of each of these commands, simply drag Jon’s Commands onto the Script Editor or use the Open Dictionary command to open it. To install it, simply drop it into the Scripting Additions folder which is inside your Extensions folder.
Jon’s Commands includes these commands:
deleteFile - delete a file, empty folder or a list of files and/or empty folders
renameFile - rename a file or folder
moveFile - move a file, folder or a list of files and/or folders to another folder
copyFile - copy a file, folder or a list of files and/or folders
sound volume - get the volume setting
set sound volume to - set the volume setting
clipboard info - get a list of data on the clipboard
set the clipboard to - put data on the clipboard
the clipboard - get data from the clipboard
execute FKEY - run an FKEY resource
screen list - describe the monitor configuration
finder selection - return the Finder’s selection
keys pressed - get a list of pressed keys
machine environment - get info about the machine via Gestalt
play sound - play sound resources, files and descriptors
run script resource - run an ‘Scpt’ or ‘scpt’ script resource
free memory - return the free memory available
the ticks - return the current value of tickCount
walk folders - walk folders and run a script on each file
set cursor to - provide feedback with the cursor
AE user interaction level - control the interaction level
choose color - use the color picker to select a color
Jon’s Commands includes these objects:
picture - for getting them off the clipboard «class PICT»
sound - for getting them off the clipboard & playing them «class snd »
screen info - for describing the monitors
environment info - for describing the machine
Jon’s Commands includes these coercions:
string to file specification - ‘TEXT’ to ‘fss ’
styled text to file specification - ‘STXT’ to ‘fss ’
international text to file specification - ‘itxt’ to ‘fss ’
Notes for specific commands:
finder selection:
This Addition gets the Finder’s selected files and folders. This works with all 7.x Finders (up to and including the scriptable Finder). Using it with any newer Finder could cause it to fail. Hopefully it won’t crash, but it probably won’t work. I don’t check for the version because it hasn’t broken yet and people were annoyed by the limitation.
You can place script applications in the Apple Menu Items folder or with either OSA Menu or OtherMenu’s script menu and use the finder selection function to have your script operate on the selected files. Way cool.
Compared to the Scriptable Finder, which returns a list of object specifiers, this returns a list of aliases (or just one).
example:
finder selection
copyFile, deleteFile, moveFile & renameFile:
These commands work on files and lists of files. You can use file references (file "disk:folder:foo"), strings of pathnames or aliases to specify the files and folders for these commands. In contrast to the Scriptable Finder, these functions don’t put up dialogs, batch their operations and use temporary memory when needed.
RenameFile can only change the name of a file or folder. The source is a file or folder reference, alias or pathname and the destination is just the new name. It cannot be a new location.
DeleteFile is permanent and immediate. Be very careful with it. I recommend using “moveFile foo to (path to trash)” if you are at all worried or inclined toward cautiousness. This can delete empty folders.
MoveFile only moves files or folders around on the disk they are on. It does NOT copy between disks. It cannot rename or replace files either. The destination is a folder, which you can specify either with a file reference, an alias or as a pathname string.
CopyFile uses temporary memory if available and tries to copy the file in 1 pass for maximum speed. The source may be either a file or a folder, or list of files and/or folders. The destination may be either a file or folder to copy to, unless the source is a folder in which case the destination must be a folder. The source folder will be copied into the destination folder. You cannot replace an existing folder so the replacing parameter is ignored when copying folders. You cannot rename a folder while copying it, so you’ll need to use renameFile after the copy completes.
If you try to replace a busy file, the copy will succeed but the busy file will be left in the temporary items folder and will appear in the Rescued Items folder in the trash after the next boot. It will then be safe to delete when you next empty the trash.
example:
copyFile "disk:folder:foo" to "disk2:folder:bar" with replacing
copyFile "disk:folder:foo" to "disk2:folder:bar" replacing yes
copyFile "disk:folder:foo" to "disk2:folder:bar" without replacing
copyFile "disk:folder:foo" to "disk2:folder:bar" replacing ask
deleteFile "disk:folder:foo"
deleteFile file "disk:folder:"
moveFile alias "disk:folder:foo" to "disk:folder2:"
renameFile alias "disk:folder:foo" to "bar"
copyFile {"disk:folder:foo", alias "disk2:folder:bar:"} to "disk3:folder:"
This loads the FKEY resource and jumps to it. It does not press command-shift-N, so it will not trigger QuicKeys macros or any function which patches things to run at FKEY-like key combinations.
example:
execute FKEY "Switch-a-roo"
execute FKEY 5
keys pressed:
Keys Pressed returns the GetKey info in the form of a list of key names in string form.
example:
keys pressed contains "option"
keys pressed = {"option","command"}
screen list:
Screen List returns a list of screen info records which describe the monitors attached to the computer. You can specify which screen is first in the returned list by using the “starting with” parameter which takes either “main screen”, “deepest screen” or “largest screen”. The main screen is the one with the menu bar on it. It will always have 0,0 as its upper left coordinate. The deepest screen is the one with the most colors and closest to the main screen if there are more than one. The largest screen is the one with the most pixels. If no parameter is specified, the main screen is returned as the first item in the list.
The screen info record has these properties:
screen size point -- The height and width of the screen.
bounds bounding rectangle -- The boundary rectangle for the screen.
color depth small integer -- The screen color depth setting.
in color boolean -- If the screen is in color (versus grayscale).
example:
screen list
number of items of (screen list)
bounds of item 1 of (screen list starting with largest screen)
machine information:
Machine Information returns a keyworded record object of common machine information from Gestalt. You can also feed it a Gestalt selector and it will return the long word response. This provides complete access to all of the Gestalt functions that exist. You can use the “check bit” parameter to return a boolean which tells if that bit is on. Bits are numbered from 1 to 32 with number 1 being the lowest or rightmost bit. In this way you can get any information from Gestalt.
The environment info record provides these properties:
machine type string -- The name of the Macintosh model.
CPU type string -- The CPU installed.
System version string -- The current System version.
FPU boolean -- Does this machine have an FPU?
Color Quickdraw boolean -- Does this machine have color Quickdraw?
AppleTalk version extended real -- The current AppleTalk version.
keyboard string -- The name of the main keyboard.
Data Access Manager boolean -- Does this machine have the Data Access Manager installed?
software power off boolean -- Does this machine turn off when shutdown?
logical RAM integer -- Amount of RAM available to the system.
physical RAM integer -- Amount of RAM installed in this machine.
active scripts integer -- Number of script systems active.
virtual memory boolean -- Is virtual memory on?
scriptable Finder boolean -- Is the scriptable Finder present?
owner name string -- The owner name in sharing setup.
sharing name string -- The machine name in sharing setup.
Both this and keys pressed use STR# resources if you are inclined to localize them. The machine info is a subset of the available Gestalt selectors. If you have need of others, let me know and we might be able to include them in a future version, although you can get raw information for any gestalt selector directly.
example:
machine information
scriptable Finder of machine information
machine information "code"
machine information "code" checking bit 0
clipboard info, the clipboard & set the clipboard to:
All of these can manage multiple data types. Styled text is the default data type, with plain text (known as string) as the backup. International text is also supported, as are a lot of other data formats. Almost any resource type can be manipulated in AppleScript as an AEDesc in applications. Pictures and sounds are also popular to move around.
There is a problem with using the clipboard in multiple programs. Thus, you can only use these commands in the front application. The simplest thing to do is use “activate” first to get the application to the front and then nab or grab the clipboard. Some applications don’t seem to check if the scrap has changed while they are running since it only changes when you switch layers in System 7. In this case you can simply activate an app and then activate the original.
example:
clipboard info
set foo to the clipboard
set foo to the clipboard as "PICT"
set the clipboard to foo
sound volume & set sound volume to:
Both of these use a number between 0 and 7 for the volume, just like everyone else.
example:
set oldVol to sound volume
set sound volume to 5
set sound volume to oldVol
play sound:
This can play sound (‘snd ’) resources installed in the system or the current open files by either name or id number. It can also play sound resources out of files and out of AppleScript variables (typically accessed via the clipboard). You cannot interrupt a sound in progress in this version and the sounds are played synchronously. This command is a small improvement on Donald’s sample code from the Language Reference Manual.
example:
play sound "I’m sorry Dave..."
run script resource:
This matches the AppleScript Run Script command but works on resources instead of files. This allows you to bundle several scripts (typically from other OSA components such as QuicKeys) into a single script file or application. I’ve used it to place the ‘scpt’ resources from QuicKeys scripts into an applet and run them from there.
There is a problem with having more than 1 ‘scpt’ resource per file and that is the Script Editor. It finds the script to display by using Get1IndResource('scpt', 1) which can return the wrong resource if you have several. If you are literate with ResEdit you can make sure the proper script resource is last in the file, but that’s less certain than the other technique I recommend, which is to change the script resource’s type to ‘Scpt’ which is not recognized by the Script Editor. You need to copy the script resource as hex data and paste it into a new empty ‘Scpt’ resource. This way allows no conflicts. Another solution is to use my ScriptServer application (available on Info-Mac & gaea) which allows you to create script resources of any type and id. This makes it simple to create a script application which combines AppleScript and QuicKeys scripts. You can even record a QuicKeys script, decompile it with ScriptServer, edit it and recompile it as a piece of a script application.
Yet another option is simply to record a QuicKeys script, copy it and paste the text into your AppleScript script with the “run script foo in QuicKeys” command with foo as your text or a variable with text in it. This works well with simple scripts and doesn’t require this osax. Unfortunately, the Run Script 1.1 osax has a memory leak when doing this. Running a compiled script resource is marginally faster too.
You can reference script resources by name or number.
example:
run script resource "Flail madly"
free memory:
This returns the current value of the function FreeMem. It shows the amount of free memory available in the current application’s current heap. Useful for finding memory leaks. You can also specify which heap you would like info about with the constants appZone and sysZone.
example:
free memory
free memory sysZone
the ticks:
This returns the current value of TickCount. This is useful for timing functions from scripts. Unfortunately, this opens the resource file for Jon’s Commands every time and thus affects your timing slightly, making it more useful for large timings where the overhead is less instead of small ones. The example is a script object which can record a series of times using the ticks. Include it at the beginning of your script and call it’s methods.
example:
script SmartTimer
property IMrunning : false
property startTime : 0
property endTime : 0
property timeLog : {}
on startTimer()
if IMrunning then
error "Timer already running"
end if
try
set startTime to the ticks
set IMrunning to true
on error theMsg number -1708
error "You need Jon’s Commands installed to use this script object."
end try
end startTimer
on stopTimer()
if not IMrunning then
error "Timer is not running"
end if
set endTime to the ticks
set IMrunning to false
set elapsedTime to endTime - startTime
set timeLog to timeLog & {elapsedTime}
return elapsedTime
end stopTimer
on averageTime()
set sum to 0
set n to number of items of timeLog
repeat with i in timeLog
set sum to sum + i
end repeat
return sum / n
end averageTime
on getLog()
return timeLog
end getLog
on clearLog()
set timeLog to {}
end clearLog
end script
walk folders:
This makes file processors very simple and fast. It takes a list of files and folders and a script. It then walks through them all and sends each file to an open handler in the script. Note that as of version 1.3.4, walk folders steps backwards through directories (reverse alphabetical order) so that you can delete files without affecting the subsequent indexing. It is possible to make this be an option if people whine enough.
The “only using files of type” parameter allows you to specify one or more file types that will be operated on. Only files of the types specified will be sent to your script parameter. The types are 4 character strings with the obscure codes you will have to find out about. Normal choices are things like "APPL", "osax" and "TEXT".
In addition, there are 2 boolean parameters, “using files” and “using folders”. “using files” defaults to true and causes all files to be run through the script. “using folders” defaults to false and causes all folders to be run through the script. You can set them to true or false by using “with using folders” and/or “without using files”.
example:
on open (theFiles)
script foo
on open (theFile)
display dialog theFile as string
end open
end script
walk folders theFiles with script foo only using files of type {"TEXT", "osas"}
end open
set cursor to:
This allows you to change the current cursor to indicate a busy script. It is problematic since the front application can change it right back, and many do, but it can be very useful nonetheless.
You can use the five standard cursors; arrow cursor, watch cursor, I beam cursor, cross cursor & plus cursor, plus a busy cursor which spins. You can also specify the name or number of a CURS resource or, using the “with cursor list” parameter, an acur resource. Subsequent calls with the “busy cursor” will increment this cursor one step.
The busy cursor keeps an acur and all the CURS resources in memory between calls. The “with cursor list” parameter is only necessary on the first call and will reinitialize the cursor if always supplied (i.e. the cursor will not spin). You can clear this memory by setting the cursor to something other than a busy cursor (i.e. watch cursor or arrow cursor).
These are the busy cursors available in Jon’s Commands, use their names or numbers:
This does not support color cursors yet.
example:
set cursor to busy cursor with cursor list "Zen Cursor"
repeat
set cursor to busy cursor
end repeat
set cursor to watch cursor
AE user interaction level:
This allows you to get and set the AppleEvent manager’s user interaction level. This way you can allow interaction from remote machines or disallow interaction from local scripts. In order for this to work, the program which is doing the interaction must explicitly check if interaction is allowed. Hopefully all scriptable applications do.
This returns the current state and then optionally sets it to the new state.
The only drawback with this command is that you cannot turn off user interaction in the current application from a script run by OSA Menu. Since the lowest level is “AE interact with self” and a script running by OSA Menu is sending events to the same application, this is always allowed so they think thy can put up dialogs. Applications need to support a “never interact” mode explicitly. Apple’s PhotoFlash does. (shameless plug from one of the authors)
example:
set oldLevel to AE user interaction level AE interact with self
AE user interaction level oldLevel
choose color:
This function displays the standard color picker and returns an RGB color value. You can specify the prompt and the initial color. RGB colors can be specified via a list of three numbers between 0 and 65535. It’s actually a 3 word data value which can be coerced to and from a list.
example:
choose color
set newColor to choose color "Pick your favorite color:" starting color {0,0,0}
example script:
-- paste an HTML color reference into BBEdit
property lastColor : {35000, 35000, 35000}
set lastColor to choose color "Choose a background color:" starting color lastColor
set s to "#"
set a to "0123456789ABCDEF"
repeat with i in lastColor
set q to round (i / 65535 * 256)
set h to q div 16
set l to q mod 16
set s to s & character (h + 1) of a & character (l + 1) of a
end repeat
tell application "BBEdit 3.5"
set selected text of window 1 to s
end tell
Coercions:
string to file specification
styled text to file specification
international text to file specification
These coercions allow you to use strings where a program or osax expects a file specification. Note that the AppleScript class “file” is really an object reference, not simply an FSSpec, although it can be coerced to one. You can create an FSSpec by using this command: “set x to "disk:folder:file" as «class fss »”. They are particularly easy to implement. With these coercions you don’t need to worry about them though.
Version History:
1.0b1 - first public release
1.0b2 - fixed key name in “keys pressed” for PowerBook keyboard.
1.0b3 - added 7.1.1 to version check for “finder selection”.
1.0b4 - added “run script resource” command.
1.0b5 - added “free memory” and “the ticks” commands.
1.0 - official release.
1.1b1 - added “copyFile” command. Made “moveFile”, “deleteFile”, “renameFile” & “copyFile” accept lists of files, aliases or pathnames. Added string to file specification coercion.
1.1b2 - fixed bug in “set clipboard to” which could cause changes to the clipboard to be unnoticed and fixed a really minor bug in “execute FKEY” which passed a dirty PC to the FKEY in 24 bit mode.
1.1b3 - added “walk folders” command, made “finder selection” return aliases for AS 1.0 compatibility and made “screen list” work without color QuickDraw.
1.1 - official release
1.1.1b1 - fixed “machine environment” property name conflict with “machine” by renaming it “machine type” and actually implemented the “cpu type” property. Also fixed the sample script for “walk folders” above.
1.1.1b2 - fixed “execute fkey” error handling, twiddled clipboard code to remove debugger calls & clean up the code a bit, fixed “finder selection” when items were on desktop of volumes other than the startup volume & made “walk folders” send an alias instead of a file specification for compatibility with AS 1.0.
1.1.1b3 - fixed error handler in “play sound”.
1.1.1 - official release
1.1.2b1 - made string to fsspec coercion more picky so that it doesn’t confuse the “run script” osax.
1.1.2 - official release
1.1.3b1 - recompiled with debugger breaks off. Ooops.
1.1.3 - official release
1.2b1 - added “set cursor to” command.
1.2b2 - added “AE user interaction level” function.
1.2 - official release
1.3b1 - added “only using files of type” parameter to “walk folders” command.
1.3 - official release
1.3.1b1 - made aete unpurgeable to avoid Script Editor dictionary bug.
1.3.1 - official release
1.3.2b1 - added “scriptable Finder” to “machine environment” record.
1.3.2b2 - added additional acur and CURS resources and spruced up the readme.
1.3.2b3 - made “copyFile” delete the temp file even if FSpExchangeFile fails. Also fixed memory leak in “keys pressed”.
1.3.2b4 - honest, this time “copyFile” is fixed. It was FSpDelete that was failing.
1.3.2b5 - replaced guts of “copyFile” with MoreFiles code. Now it works with drop boxes. Spruced up the readme some more.
1.3.2 - official release
1.3.3b1 - Made clipboard commands error when in the background.
1.3.3b2 - Fixed “copyFile” problem when replacing a nonexistent file.
1.3.3 - official release
1.3.4b1 - Made “walk folders” command step backwards through folders.
1.3.4b2 - Made “copyFile” fail when copying to a nonexistant directory.
1.3.4b3 - Made “copyFile” return better errors when files and directories are missing. Zeroed location when not replacing so that the Finder will put the icon in the first empty spot.
1.3.4b4 - Made “copyFile” copy folders too.
1.3.4 - official release
1.3.5b1 - Fixed “copyFile” bug when copying file to file with a different name.
1.3.5 - official release
1.3.6b1 - Made replacing parameter on “copyFile” accept yes/no/ask & error when used with directories.
1.3.6b2 - Fixed “walk folders” bug when walking folders; files were fine.
1.3.6b3 - Fixed “finder selection” crash when trash is selected. There are probably still problems with selecting PowerTalk items.
1.3.6b4 - Added “check bit” parameter to “machine environment” command.
1.3.6 - official release
1.3.7b1 - Fixed some comments in the dictionary. Changed the “check bit” parameter of “machine environment” command to be zero based instead of one based, making it compatible with Inside Mac. This affects scripts!
1.4b2 - Added “choose color” command. Wondered if this change log was worth the bother. It’s kind of embarassing, but I guess it’s educational. I hope. :) Changed the version to 1.4 since we added a command.
1.4b3 - Added “owner name” and “sharing name” properties to “machine information” record.
1.4b4 - Fixed “CPU type” and “FPU” to work properly with PowerPC machines and added “PowerPC” property. Added description and examples for “screen list” command to this document. It was always missing and no one noticed.
1.4 - official release
Feel free to send money or to write with questions, comments or suggestions.